package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.entity.Admin;
import com.atguigu.result.Result;
import com.atguigu.service.AdminService;
import com.atguigu.service.RoleService;
import com.atguigu.util.QiniuUtil;
import com.github.pagehelper.PageInfo;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;
import java.util.UUID;

@Controller
@RequestMapping("admin")
public class AdminController extends BaseController {
    @Reference
    private AdminService adminService;
    @Reference
    private RoleService roleService;







    @RequestMapping
    public String findPage(HttpServletRequest request, Model model) {
        Map<String, Object> filters = getFilters(request);
        PageInfo<Admin> page = adminService.findPage(filters);
        model.addAttribute("page", page);
        model.addAttribute("filters", filters);
        return "admin/index";
    }

    @RequestMapping("create")
    public String create() {
        return "admin/create";
    }

    @RequestMapping("save")
    public String save(Admin admin, Model model) {
        Integer row = adminService.insert(admin);
        return "common/successPage";
    }


    /*修改*/
    @RequestMapping("edit/{id}")
    public String edit(@PathVariable Long id, Model model) {
        Admin admin = adminService.getById(id);

        model.addAttribute("admin", admin);
        return "admin/edit";
    }

    @RequestMapping("update")
    public String update(Admin admin) {
        Integer update = adminService.update(admin);
        return "common/successPage";
    }

    /*删除*/
    @RequestMapping("delete/{id}")
    public String delete(@PathVariable Long id) {
        adminService.delete(id);
        return "common/successPage";
    }
    /*添加头像，跳转到添加头像页面，带id过去*/

    @RequestMapping("uploadShow/{id}")
    public String uploadShow(@PathVariable Long id, Model model) {
        model.addAttribute("id", id);
        return "admin/upload";
    }

    @RequestMapping("upload/{id}")
    public String upload(@PathVariable Long id, @RequestParam(value = "file") MultipartFile file) throws IOException {

        String newFileName = UUID.randomUUID().toString();
        // 上传图片
        QiniuUtil.upload2Qiniu(file.getBytes(), newFileName);
        String url = "http://rigj16zfd.hn-bkt.clouddn.com/" + newFileName;

        Admin adminAgo = adminService.getById(id);

        String headUrl = adminAgo.getHeadUrl();
        String[] split = headUrl.split("/");
        for (int i = 0; i < split.length; i++) {
            System.out.println(i+"="+split[i]);
        }
        QiniuUtil.deleteFileFromQiniu(split[3]);

        System.out.println(headUrl);
        Admin admin = new Admin();
        admin.setHeadUrl(url);
        admin.setId(id);
        adminService.update(admin);

        return "common/successPage";

    }
    @RequestMapping("assignShow/{adminId}")
    public String assignShow(@PathVariable Long adminId ,Model model){
        /*获得该用户的权限*/

        Map<String, Object> roleMap = roleService.findRoleByAdminId(adminId);
        model.addAllAttributes(roleMap);
        model.addAttribute("adminId",adminId);

        return "admin/assignShow";
    }

    @RequestMapping("assignRole")
    public  String assignShow(Long adminId ,Long [] roleIds) {
        roleService.saveUserRoleRealtionShip(adminId,roleIds);
        return "common/successPage";
    }




}

